package com.daihou.BCJ.decrease;

import com.alibaba.fastjson.JSON;
import com.daihou.BCJ.repayment.BalanceRepayment;
import com.daihou.config.TheAuth;
import com.daihou.model.ceres.DecreaseUrlEnum;
import com.daihou.utils.DHConfigFile;
import com.daihou.utils.DHHttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author liujintao
 * @date 2021/2/8 10:37
 */
public class DecreaseOverDue {
    private static final Logger logger = LoggerFactory.getLogger(BalanceRepayment.class);
    private String url;
    Map<String, String> headers = new HashMap<>();
    private int id;

    @BeforeTest(description = "测试准备工作,获取Authorization对象")
    public void beforeTest() throws IOException {
        url = DHConfigFile.getUrl("DaiHou_URL", "decrease", DecreaseUrlEnum.DECREASEOVERDUEADD.toString());
        //设置请求头中的Authorization
        headers.put("Authorization", TheAuth.getAuth());
        headers.put("X-UserName", "admin");
    }

    @Test(description = "减免逾期天数配置新增")
    public void decreaseOverDueAddTest() {
        String body = "{\"productTypeCode\":\"HAOHUAN\",\"overdueDaysLower\":\"1000\",\"overdueDaysUpper\":\"\",\"isHaveOverdueDaysUpper\":false,\"classifyId\":6}";
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body, headers)).toJSONString();
        logger.info("减免分类列表新增结果：{}", result);
        Assert.assertTrue(result.contains("\"message\":\"操作成功\",\"status\":\"0\""));
    }

    @Test(description = "减免逾期天数配置查询", dependsOnMethods = "decreaseOverDueAddTest")
    public void decreaseOverDueQueryTest() {
        url = DHConfigFile.getUrl("DaiHou_URL", "decrease", DecreaseUrlEnum.DECREASEOVERDUEQUERY.toString());
        String body = "{\"pageNum\":1,\"pageSize\":20,\"order\":null,\"productTypeCode\":\"HAOHUAN\",\"fundItemCode\":\"T0_EARLY_SERVICE_FEE_OVERDUE_INTEREST\"}";
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body, headers)).toJSONString();
        logger.info("减免分类列表查询结果：{}", result);
        Assert.assertTrue(result.contains("\"message\":\"操作成功\",\"status\":\"0\""));
        id = JSON.parseObject(result).getJSONObject("data").getJSONArray("list").getJSONObject(0).getIntValue("id");
    }

    @Test(description = "减免逾期天数配置更新", dependsOnMethods = "decreaseOverDueQueryTest")
    public void decreaseOverDueUpdateTest() {
        url = DHConfigFile.getUrl("DaiHou_URL", "decrease", DecreaseUrlEnum.DECREASEOVERDUEUPDATE.toString());
        String body = "{\"productTypeCode\":\"HAOHUAN\",\"overdueDaysLower\":1000,\"overdueDaysUpper\":null,\"isHaveOverdueDaysUpper\":false,\"classifyId\":6,\"id\":" + id + ",\"productTypeName\":\"好还\",\"fundItemIdList\":[10],\"pageIndex\":1}";
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body, headers)).toJSONString();
        logger.info("减免分类列表查询结果：{}", result);
        Assert.assertTrue(result.contains("\"message\":\"操作成功\",\"status\":\"0\""));
    }

    @Test(description = "减免逾期天数配置删除", dependsOnMethods = "decreaseOverDueUpdateTest")
    public void decreaseOverDueDeleteTest() {
        url = DHConfigFile.getUrl("DaiHou_URL", "decrease", DecreaseUrlEnum.DECREASEOVERDUEDELETE.toString());
        String body = "{\"id\":" + id + "}";
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body, headers)).toJSONString();
        logger.info("减免分类列表查询结果：{}", result);
        Assert.assertTrue(result.contains("\"message\":\"操作成功\",\"status\":\"0\""));
    }
}